// 739.每日温度
package LeetcodeHot100;

import java.util.Deque;
import java.util.LinkedList;

public class Solution739 {
    public int[] dailyTemperatures(int[] temperatures) {
        int[] result = new int[temperatures.length];
        Deque<int[]> stack = new LinkedList<>();
        for (int i = 0; i < temperatures.length; i++) {
            while (!stack.isEmpty() && temperatures[i] > stack.peek()[0]) {
                int[] pop = stack.pop();
                result[pop[1]] = i - pop[1];
            }
            stack.push(new int[] { temperatures[i], i });
        }
        while (!stack.isEmpty()) {
            int[] pop = stack.pop();
            result[pop[1]] = 0;
        }
        return result;
    }
}
